package com.mybatisplus.controller;

import com.mybatisplus.pojo.Competition;
import com.mybatisplus.service.CompetitionService;
import com.mybatisplus.utils.ResultOBJ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
@RequestMapping("/adminapi/competitions")
public class CompetitionController {
    @Autowired
    private CompetitionService competitionService;

    @GetMapping
    public List<Competition> getCompetitionList(Competition competition) {
        return competitionService.getCompetitionList(competition);
    }

    @PostMapping
    public ResultOBJ addCompetition(@RequestBody Competition competition) {
        try {
            competitionService.addCompetition(competition);
            return ResultOBJ.ADD_SUCCESS;
        } catch (Exception e) {
            return ResultOBJ.ADD_ERROR;
        }
    }

    @PutMapping(value = "/{id}")
    public ResultOBJ updateCompetition(@PathVariable Integer id, @RequestBody Competition competition) {
        try {
            competition.setId(id);
            competitionService.updateCompetition(competition);
            return ResultOBJ.UPDATE_SUCCESS;
        } catch (Exception e) {
            return ResultOBJ.UPDATE_ERROR;
        }
    }

    @DeleteMapping(value = "/{id}")
    public ResultOBJ deleteCompetition(@PathVariable Integer id) {
        try {
            competitionService.deleteCompetition(id);
            return ResultOBJ.DELETE_SUCCESS;
        } catch (Exception e) {
            return ResultOBJ.DELETE_ERROR;
        }
    }


}
